// pages/main/main.js
var startPoint;
var startEvent;
var admin;
const app = getApp();
var longorshort = "short";
var OpenID;
export function $parseVars2Str(...args) {
  // return Array.prototype.join.call(arguments , ',');
  return args.join(",")
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHide: false,
    iconSearch: "../../images/recycle1.png",
    x: 0,
    y: 0,
    inputPlace: '搜一搜',
    deleteHide: false,
    updateHide: false,
    rsHide: true,
    searchHide: false,
    addBtnHide: false,
    weather: '',
    moveWidth: '',
    heightBtn: '',
    windowWidth: '',
    windowHeight: '',
    bgAddC: '',
    listwidth: 675,
    Lists: [],
  },
  inputChange: function(e) {
    let lists = this.data.Lists;
    // console.log(e.detail)
    let title;
    let str = e.detail.value;
    // console.log(e.detail)
    for (let i = 0; i < lists.length; i++) {
      title = lists[i].title;
      if (title.search(str) >= 0 || str == "") {
        lists[i].listHide = false;
      } else {
        lists[i].listHide = true;
      }
    }
    this.setData({
      Lists: lists,
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        // console.log(res.windowWidth);
        // console.log(res.windowHeight)
        that.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight,
          heightBtn: res.windowHeight - 58,
          moveWidth: res.windowWidth * 0.9 - 97,
          // leftBtn: res.windowWidth-57,

        })
      },
    })
    // this.getLocation();


    this.Show(1);

  },
  setUser: function(OpenID) {
    let that = this;
    console.log(that.data.Lists.length);
    let db = wx.cloud.database();
    db.collection("User").where({
      _openid: OpenID,
      live: 1,
    }).get().then(function(res) {
      // console.log(res.data[0].admin)
      // if (res.data[0].admin=="true"){
        admin = res.data[0].admin;
        console.log(admin)
      // }
      if (res.data.length > 0) {
        wx.cloud.callFunction({
          name: 'runDB',
          data: {
            type: "update",
            db: "User",
            condition: {
              _openid: OpenID,
            },
            data: {
              FileNum: that.data.Lists.length,
            }
          },
          success: function(res) {
            console.log(res.result.stats);
          },
          fail: function(err) {
            console.log(err);
          }
        })
      } else {
        wx.cloud.callFunction({
          name: "runDB",
          data: {
            type: "insert",
            db: "User",
            data: {
              FileNum: 0,
              live: 1,
              _openid: OpenID,
              disTXT: "不让你看了"
            }
          },
          success: function(res) {
            console.log(res)
          },
          fail: function(err) {
            console.log(err)
          }
        })
      }

    })
  }, //第二个参数判断是回收站还是正常显示，1位正常，0为回收站,2为永久删除的(暂时没有要获取的)
  getYun: function(openID, gc) {
    let that = this;
    // let db = wx.cloud.database();
    // db.collection('User')
    //   .where({
    //     _id: "bea5de3e-96a1-4b8a-a4e2-c730d0998781",
    //   }).field({
    //     getid: true,
    //   })
    //   .get({
    //     success:function(res) {
    //       console.log(res)
    //     }
    //   })
    if (admin=="true") {
      console.log("openid:" + openID)
      wx.cloud.callFunction({
        name: "runDB",
        data: {
          db: "Menu",
          type: "get1",
          condition: {
            status: gc,
          },

          field: {
            title: true,
            _id: false,
            _openid:true,
            time: true,
            listHide: true,
            day: true,
            city: true,
            weather: true,
          },
        },
        success: res => {
          console.log(res.result.data)
          // wx.cloud.callFunction({
          //   name: "runDB",
          //   data: {
          //     type: "get2",
          //     db: "Menu",
          //     addtoset: res.result.data
          //   },
          //   success: function (res) {
          //     console.log(res)
          //   },
          //   fail: function (e) {
          //     console.log(e)
          //   }
          // })
          that.setData({
            Lists: res.result.data
          })
          this.getTextinfo(gc);
        },
        fail: err => {
          console.error("error:" + err)
        }
      })
    } else {
      console.log("openid:" + openID)
      wx.cloud.callFunction({
        name: "runDB",
        data: {
          db: "Menu",
          type: "get1",
          condition: {
            _openid: openID,
            status: gc,
          },

          field: {
            _openid: true,
            title: true,
            _id: false,
            time: true,
            listHide: true,
            day: true,
            city: true,
            weather: true,
          },
        },
        success: res => {
          console.log(res)
          // wx.cloud.callFunction({
          //   name: "runDB",
          //   data: {
          //     type: "get2",
          //     db: "Menu",
          //     addtoset: res.result.data
          //   },
          //   success: function (res) {
          //     console.log(res)
          //   },
          //   fail: function (e) {
          //     console.log(e)
          //   }
          // })
          that.setData({
            Lists: res.result.data
          })
          this.getTextinfo(gc);
        },
        fail: err => {
          console.error("error:" + err)
        }
      })
    }
  },

  btnSearch: function() {
    let url;
    if (this.data.iconSearch == "../../images/recycle1.png") {
      url = "../../images/recycle2.png";
      this.recycle();
    } else if (this.data.iconSearch == "../../images/recycle2.png") {
      url = "../../images/recycle1.png";
      this.setData({
        deleteHide: false,
        updateHide: false,
        rsHide: true,
      })
      this.Show(1);
    }
    this.setData({
      iconSearch: url,
    })
    console.log(this.data)

  },
  recycle: function() {
    this.setData({
      deleteHide: true,
      updateHide: true,
      rsHide: false,
    })
    this.Show(0);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  onPullDownRefresh: function() {
    if (this.data.iconSearch == "../../images/recycle1.png") {
      this.Show(1);
    } else {
      this.Show(0);
    }
  },

  Show: function(gc) {
    longorshort = "short";
    this.setData({
      isHide: false,
    })
    wx.cloud.callFunction({
      name: 'getOpenid',
      success: res => {
        this.getYun(res.result.openid, gc);
        OpenID = res.result.openid;
      }
    })

    this.setData({
      x: this.data.windowWidth - 57,
      y: this.data.windowHeight - 211,
    })

    wx.stopPullDownRefresh();
  },
  getTextinfo: function(gc) {
    var that = this;
    const query = wx.createSelectorQuery();
    for (let i = 0; i < this.data.Lists.length; i++) {
      query.select('#A' + this.data.Lists[i].time).boundingClientRect()
    }
    query.exec(function(rect) {
      console.log(rect)
      let List = that.data.Lists;
      for (let i = 0; i < that.data.Lists.length; i++) {
        List[i].autoheight = rect[i].height + 80;
      }
      //判断用户live是否为0，为0者啥也看不见
      // let that = this;
      let db = wx.cloud.database();
      db.collection("User").where({
        _openid: OpenID,
        live: 0,
      }).get().then(function(res) {
        console.log(res)
        if (res.data.length > 0) {
          that.setData({
            Lists: [],
            inputPlace: res.data[0].disTXT,
            searchHide: true,
            addBtnHide: true,
          })
        } else {
          that.setData({
            Lists: List,
            isHide: true,
            inputPlace: '搜一搜',
            searchHide: false,
            addBtnHide: false,
          })
        }
      })

    });
    if (gc == 1) {
      this.setUser(OpenID);
    }
  },
  moveXzero: function(e, fi) {
    let lists = this.data.Lists;
    for (let i = 0; i < this.data.Lists.length; i++) {
      if (i != e.currentTarget.dataset.listindex && fi == true) {
        lists[i].xmove = 0;
      }
    }
    this.setData({
      Lists: lists,
    })
  },
  handleMovableChange: function(e) {
    var that = this;
    if (e.detail.source === 'friction') {
      if (e.detail.x < that.data.windowWidth * 0.9 * 0.17 * (-1)) {
        this.ShowDelete(e)
      } else {
        this.HideDelete(e)
      }
    }

  },
  HideDelete: function(e) {
    let index = e.currentTarget.dataset.listindex;
    this.setXmove(index, 0);
    this.moveXzero(e, true);
  },
  setXmove: function(index, x) {
    let lists = this.data.Lists;
    lists[index].xmove = x;
    this.setData({
      Lists: lists,
    })
  },
  ShowDelete: function(e) {
    let index = e.currentTarget.dataset.listindex;
    this.setXmove(index, this.data.windowWidth * 0.9 * 0.34 * (-1));
    this.moveXzero(e, true);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  rsbtnlongtap: function(e) {
    console.log(e)
    if (this.data.iconSearch == "../../images/recycle2.png") {
      let that = this;
      wx.showModal({
        title: '问',
        content: '确认永久删除吗？',
        success: function(res) {
          if (res.confirm) {
            that.deleteInfo(e, -1);
          }
        }
      })
    }
  },
  rsList: function(e) {
    let lists = this.data.Lists;
    let Time = parseInt(lists[e.currentTarget.dataset.delid].time);
    // console.log(Time)
    wx.cloud.callFunction({
      name: "runDB",
      data: {
        type: "update",
        db: "Menu",
        condition: {
          "time": Time,
        },
        data: {
          "status": 1
        }
      },
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.error("error:" + err)
      }
    })
    lists.splice(e.currentTarget.dataset.delid, 1);
    this.setData({
      Lists: lists,
    })
  },
  deleteList: function(e) {
    this.deleteInfo(e, 0);

  },
  deleteInfo: function(e, sta) {
    let lists = this.data.Lists;
    let Time = parseInt(lists[e.currentTarget.dataset.delid].time);
    // console.log(Time)
    wx.cloud.callFunction({
      name: "runDB",
      data: {
        type: "update",
        db: "Menu",
        condition: {
          "time": Time,
        },
        data: {
          "status": sta
        }
      },
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.error("error:" + err)
      }
    })
    lists.splice(e.currentTarget.dataset.delid, 1);
    this.setData({
      Lists: lists,
    })
  },
  //修改
  updateList: function(e) {
    let lists = this.data.Lists;
    lists[e.currentTarget.dataset.delid].xmove = 0;
    this.setData({
      Lists: lists,
    })
    let Time = this.data.Lists[e.currentTarget.dataset.delid].time;
    let OPENID = this.data.Lists[e.currentTarget.dataset.delid]._openid;
    wx.navigateTo({
      url: '../List/List?time=' + Time + '&openid=' + OPENID,
    })
    success: res => {

    }
  },
  //添加
  btnAdd: function() {
    if (longorshort == "short") {
      this.setData({
        bgAddC: 'rgb(233, 233, 233)'
      })
      wx.navigateTo({
        url: '../List/List',
      })
      var that = this;
      setTimeout(function() {
        that.setData({
          bgAddC: 'white'
        })
      }, 100)
    }
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  btnShowBook: function(e) {
    if (this.data.iconSearch == "../../images/recycle1.png") {
      let lists = this.data.Lists;
      lists[e.currentTarget.dataset.delid].xmove = 0;
      this.setData({
        Lists: lists,
      })
      let Time = this.data.Lists[e.currentTarget.dataset.delid].time;
      let OPENID = this.data.Lists[e.currentTarget.dataset.delid]._openid;
      wx.navigateTo({
        url: '../Show/Show?time=' + Time + '&openid=' + OPENID + "&method=" + "tap",
      })
      success: res => {

      }
    }

  }
})